package com.yanqu.road.server.manger.maidian.resource;

import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ResourceChangeLog {

    //资源变动  缓存
    public static Map<Long, List<Map<String, Object>>> resourceChangeMap = new ConcurrentHashMap<>();

    public static void logoutUploadResourceChange(GamePlayer player){

        List<Map<String, Object>> list = DataAnalyticsMgr.popUserMapData(player.getUserId(), resourceChangeMap);

        if(list == null || list.size() == 0){
            return;
        }

        trackResourceChange(player, list);

    }

    /**
     * 资源变动
     */
    public static void trackResourceChange(GamePlayer player, int itemId, int type, long changeValue, long oldValue, int reason) {

        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            List<Map<String, Object>> list = DataAnalyticsMgr.getUserMapDataForNew(player.getUserId(), resourceChangeMap);

            synchronized (list){
                try {
                    Map<String, Object> map = new HashMap<>();
                    map.put(EventDataConstants.Resource.RESOURCE_ID, itemId);
                    map.put(EventDataConstants.Resource.CHANGE_TYPE, type);
                    map.put(EventDataConstants.Resource.CHANGE_NUM, changeValue);
                    map.put(EventDataConstants.Resource.CHANGE_REASON, reason);
                    list.add(map);
                }catch (Exception e){
                    DataAnalyticsMgr.getLogger().error("商品模块 购买道具 itemId:" + itemId, e);
                }

                if(list.size() + 1 >= EventDataConstants.ARRAY_OBJECT_MAX_SIZE){
                    trackResourceChange(player, list);
                }
            }

        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error("analytics trackResourceChange error ", e);
        }
    }

    private static void trackResourceChange(GamePlayer player, List<Map<String, Object>> list) {

        try{
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(EventDataConstants.Resource.RESOURCE_CHANGE, player);
            traceAction.build(EventDataConstants.Resource.RESOURCE_CHANGE_INFO, list).trace();
        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error(" trackResourceChange ", e);
        }

    }

    /**
     * 资源获得
     */
    public static void trackResourceGetSingle(GamePlayer player, GoodsInfo goodInfo, BigInteger goodsCount, eLogMoneyType son, long oldValue, long nowValue){
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(EventDataConstants.Resource.RESOURCE_GET, player);
            traceAction.build(EventDataConstants.Resource.RESOURCE_ID, goodInfo.getGoodsId());
            traceAction.build(EventDataConstants.Resource.CHANGE_REASON, son.getValue());
            traceAction.build(EventDataConstants.Resource.CHANGE_NUM, goodsCount.longValue());
            traceAction.build(EventDataConstants.Resource.CHANGE_BEFORE, oldValue);
            traceAction.build(EventDataConstants.Resource.CHANGE_AFTER, nowValue);
            //traceAction.build(EventDataConstants.Resource.RESOURCE_NAME, goodInfo.getGoodsName());
            traceAction.trace();
        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error("error ", e);
        }
    }

    /**
     * 资源使用
     */
    public static void trackResourceUseSingle(GamePlayer player, int goodsId, eLogMoneyType son, BigInteger goodsCount, long oldValue, long nowValue){
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(EventDataConstants.Resource.RESOURCE_USE, player);
            traceAction.build(EventDataConstants.Resource.RESOURCE_ID, goodsId);
            traceAction.build(EventDataConstants.Resource.CHANGE_REASON, son.getValue());
            traceAction.build(EventDataConstants.Resource.CHANGE_NUM, goodsCount.longValue());
            traceAction.build(EventDataConstants.Resource.CHANGE_BEFORE, oldValue);
            traceAction.build(EventDataConstants.Resource.CHANGE_AFTER, nowValue);
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
            if(goodsInfo != null){
                traceAction.build(EventDataConstants.Resource.RESOURCE_NAME, goodsInfo.getGoodsName());
            }
            traceAction.trace();
        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error("error ", e);
        }
    }

    /**
     * 资源获得
     */
    public static void trackResourceGetSingle(GamePlayer player, int goodsId, String goodsName, BigInteger goodsCount, eLogMoneyType son, long oldValue, long nowValue){
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(EventDataConstants.Resource.RESOURCE_GET, player);
            traceAction.build(EventDataConstants.Resource.RESOURCE_ID, goodsId);
            traceAction.build(EventDataConstants.Resource.CHANGE_REASON, son.getValue());
            traceAction.build(EventDataConstants.Resource.CHANGE_NUM, goodsCount.longValue());
            traceAction.build(EventDataConstants.Resource.CHANGE_BEFORE, oldValue);
            traceAction.build(EventDataConstants.Resource.CHANGE_AFTER, nowValue);
            //traceAction.build(EventDataConstants.Resource.RESOURCE_NAME, goodsName);
            traceAction.trace();
        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error("error ", e);
        }
    }

}
